<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>

    <link rel="stylesheet" type="text/css" 
      href="/assets/css/straybirds.css" media="screen" />
    <link rel="stylesheet" type="text/css" 
      href="/assets/css/pygments.css" media="screen" />

    <!-- MathJax Section Start -->

    <script type="text/javascript"
    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
    </script>
    <script>
        MathJax.Hub.Config({
              tex2jax: {
              skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
              }
        });
        MathJax.Hub.Queue(function() {
            var all = MathJax.Hub.getAllJax(), i;
            for(i=0; i < all.length; i += 1) {
                all[i].SourceElement().parentNode.className += ' has-jax';
            }
        });
    </script>

    <!-- MathJax Section End -->

    <!-- Google Analytics Start-->
    <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-48100787-1', 'minixalpha.github.io');
  ga('send', 'pageview');

</script>

    <!-- Google Analytics End -->

    <title>首页</title>
  </head>

  <body>
    <div class="container">
      <header>
        <div class="container">
          <h1>
              <a href="/" title="Home Page"> 潇湘夜雨 </a>
          <span class="github-src">
            <a href ="https://github.com/minixalpha/minixalpha.github.io"
               target="_blank"
               title="Fork me on GitHub">
              <img src="/assets/images/GitHub-Mark-Light-32px.png" alt="">
            </a>
          </span>
          </h1>
        </div>
      </header>

      <aside id="left-side">
        <h2> 分类 </h2>
  <ul class="category-list">
      
            
                <li>
                <a href="/categories/源代码阅读"> 源代码阅读 (20) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/设计模式"> 设计模式 (3) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/科研"> 科研 (6) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/思想"> 思想 (2) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/技术"> 技术 (18) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/工具"> 工具 (4) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/虚拟机"> 虚拟机 (1) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/java"> java (11) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/c语言"> c语言 (4) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/读书"> 读书 (1) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/操作系统"> 操作系统 (1) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/英语"> 英语 (10) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/计算机系统"> 计算机系统 (3) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/生活"> 生活 (1) </a>
                </li>
            
      
  </ul>

      </aside>

      <aside id="right-side">
        <h2> 归档 </h2>
  <ul class="archive-list">
    
    
    
        
        
        
        
            
            <li>
                <a href="/2014/08">
                    2014-08 (1)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/07">
                    2014-07 (5)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/05">
                    2014-05 (12)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/04">
                    2014-04 (3)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/03">
                    2014-03 (11)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/02">
                    2014-02 (6)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/01">
                    2014-01 (3)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2013/11">
                    2013-11 (10)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2013/10">
                    2013-10 (3)
                </a>
            </li>

        
        
    
        
        
        
        
            
            <li>
                <a href="/2010/09">
                    2010-09 (1)
                </a>
            </li>

        
        
    
  </ul>

      </aside>

      <article>

<ul class="post-list">
    
    <li>
      <div class="post-title">
        <a href="/%E8%8B%B1%E8%AF%AD/2014/05/25/learn-english-1314.html"> 学习英语第十三十四周</a> </br>
      </div>

      <div class="post-info">
        <ol>
          <li class="post-time">2014-05-25</li> 
          <li class="post-category"> 
           英语
          </li>
        </ol>
      </div>

      <div class="post-preview">
        <h1>学习英语第十三十四周</h1>

<p>标签：英语</p>

<h2>阅读英文原著</h2>

<table><thead>
<tr>
<th align="center">Book</th>
<th align="center">Time</th>
<th align="center">Progress</th>
</tr>
</thead><tbody>
<tr>
<td align="center">Harry Potter And The Goblet of Fire</td>
<td align="center">2014.05.12 ~ 2014.05.23</td>
<td align="center">734/734</td>
</tr>
</tbody></table>
 
        <a href="/%E8%8B%B1%E8%AF%AD/2014/05/25/learn-english-1314.html"> Read More ... </a> </br>
      </div>

    </li>
    
    <li>
      <div class="post-title">
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/24/read-openjdk-src-ArrayList.html"> OpenJDK 源码阅读之 ArrayList</a> </br>
      </div>

      <div class="post-info">
        <ol>
          <li class="post-time">2014-05-24</li> 
          <li class="post-category"> 
           技术&nbsp&nbspjava&nbsp&nbsp源代码阅读
          </li>
        </ol>
      </div>

      <div class="post-preview">
        <h1>OpenJDK 源码阅读之 ArrayList</h1>

<hr>

<h2>概要</h2>

<ul>
<li>类继承关系 </li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="n">java</span><span class="o">.</span><span class="na">lang</span><span class="o">.</span><span class="na">Object</span>
    <span class="n">java</span><span class="o">.</span><span class="na">util</span><span class="o">.</span><span class="na">AbstractCollection</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span>
        <span class="n">java</span><span class="o">.</span><span class="na">util</span><span class="o">.</span><span class="na">AbstractList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span>
            <span class="n">java</span><span class="o">.</span><span class="na">util</span><span class="o">.</span><span class="na">ArrayList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span>
</code></pre></div>
<ul>
<li>定义</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="n">AbstractList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span>
        <span class="kd">implements</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;,</span> <span class="n">RandomAccess</span><span class="o">,</span> <span class="n">Cloneable</span><span class="o">,</span> <span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">Serializable</span>
<span class="o">{</span>
<span class="o">}</span>
</code></pre></div> 
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/24/read-openjdk-src-ArrayList.html"> Read More ... </a> </br>
      </div>

    </li>
    
    <li>
      <div class="post-title">
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/23/read-openjdk-src-String.html"> OpenJDK 源码阅读之 String</a> </br>
      </div>

      <div class="post-info">
        <ol>
          <li class="post-time">2014-05-23</li> 
          <li class="post-category"> 
           技术&nbsp&nbspjava&nbsp&nbsp源代码阅读
          </li>
        </ol>
      </div>

      <div class="post-preview">
        <h1>OpenJDK 源代码阅读之 String</h1>

<hr>

<h2>概要</h2>

<ul>
<li>类继承关系</li>
</ul>
<div class="highlight"><pre><code class="text language-text" data-lang="text">java.lang.Object
    java.lang.String
</code></pre></div>
<ul>
<li>定义</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="nc">String</span>
<span class="kd">extends</span> <span class="n">Object</span>
<span class="kd">implements</span> <span class="n">Serializable</span><span class="o">,</span> <span class="n">Comparable</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;,</span> <span class="n">CharSequence</span>
</code></pre></div>
<ul>
<li>要点</li>
</ul>

<p>一旦创建就不可改变</p>

<h2>实现</h2>

<ul>
<li>storage</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="cm">/** The value is used for character storage. */</span>
<span class="kd">private</span> <span class="kd">final</span> <span class="kt">char</span> <span class="n">value</span><span class="o">[];</span>
</code></pre></div>
<p>可以看出 <code>String</code> 中的数据是如何存储的。</p>

<ul>
<li>初始化</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">public</span> <span class="nf">String</span><span class="o">(</span><span class="n">String</span> <span class="n">original</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">original</span><span class="o">.</span><span class="na">value</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">hash</span> <span class="o">=</span> <span class="n">original</span><span class="o">.</span><span class="na">hash</span><span class="o">;</span>
    <span class="o">}</span>
</code></pre></div>
<p>可以看出使用 <code>String</code> 类型初始化，新 <code>String</code> 实际上与原来的 <code>String</code> 指向同一块内存。</p>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="kd">public</span> <span class="nf">String</span><span class="o">(</span><span class="kt">char</span> <span class="n">value</span><span class="o">[])</span> <span class="o">{</span>
    <span class="k">this</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">copyOf</span><span class="o">(</span><span class="n">value</span><span class="o">,</span> <span class="n">value</span><span class="o">.</span><span class="na">length</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div>
<p>如果用 <code>char[]</code> 初始化，可以看出，新分配了内存，并复制，保证了两者相互独立，只是内容相同。</p>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">public</span> <span class="nf">String</span><span class="o">(</span><span class="n">StringBuffer</span> <span class="n">buffer</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">synchronized</span><span class="o">(</span><span class="n">buffer</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">copyOf</span><span class="o">(</span><span class="n">buffer</span><span class="o">.</span><span class="na">getValue</span><span class="o">(),</span> <span class="n">buffer</span><span class="o">.</span><span class="na">length</span><span class="o">());</span>
        <span class="o">}</span>
    <span class="o">}</span>
</code></pre></div>
<p>注意用 <code>StringBuffer</code> 初始化时，对同一 <code>buffer</code> 是线程安全的，即初始化 <code>String</code> 的过程中，其它线程不会改变 <code>buffer</code> 的内容。</p>

<p>另外，能告诉我下面这段代码是怎么回事么？</p>
<div class="highlight"><pre><code class="java language-java" data-lang="java">   <span class="kd">public</span> <span class="nf">String</span><span class="o">(</span><span class="n">StringBuilder</span> <span class="n">builder</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">copyOf</span><span class="o">(</span><span class="n">builder</span><span class="o">.</span><span class="na">getValue</span><span class="o">(),</span> <span class="n">builder</span><span class="o">.</span><span class="na">length</span><span class="o">());</span>
    <span class="o">}</span>
</code></pre></div>
<p>为啥这次不同步了呢？</p>
 
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/23/read-openjdk-src-String.html"> Read More ... </a> </br>
      </div>

    </li>
    
    <li>
      <div class="post-title">
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/22/read-openjdk-src-HashSet.html"> OpenJDK 源码阅读之 HashSet</a> </br>
      </div>

      <div class="post-info">
        <ol>
          <li class="post-time">2014-05-22</li> 
          <li class="post-category"> 
           技术&nbsp&nbspjava&nbsp&nbsp源代码阅读
          </li>
        </ol>
      </div>

      <div class="post-preview">
        <h1>OpenJDK 源代码阅读之 HashSet</h1>

<hr>

<h2>概要</h2>

<ul>
<li>类继承关系</li>
</ul>
<div class="highlight"><pre><code class="text language-text" data-lang="text">java.lang.Object
    java.util.AbstractCollection&lt;E&gt;
        java.util.AbstractSet&lt;E&gt;
            java.util.HashSet&lt;E&gt;
</code></pre></div>
<ul>
<li>定义</li>
</ul>
<div class="highlight"><pre><code class="text language-text" data-lang="text">public class HashSet&lt;E&gt;
extends AbstractSet&lt;E&gt;
implements Set&lt;E&gt;, Cloneable, Serializable
</code></pre></div>
<ul>
<li>要点</li>
</ul>

<ol>
<li>不保证元素次序，甚至不保证次序不随时间变化</li>
<li>基本操作(add, remove, contains, size)常量时间</li>
<li>迭代操作与当前元素个数加底层容量大小成正比</li>
<li>不保证同步</li>
</ol>

<h2>思考</h2>

<ul>
<li>总体实现</li>
</ul>

<p>底层是用 <code>HashMap</code> 实现的，<code>Set</code> 中的数据是 <code>HashMap</code> 的 <code>key</code>，所有的 <code>key</code> 指向同一个 <code>value</code>, 此 <code>value</code> 定义为：</p>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="c1">// Dummy value to associate with an Object in the backing Map</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">Object</span> <span class="n">PRESENT</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Object</span><span class="o">();</span>
</code></pre></div>
<p>再看一下 <code>add</code>，大概就能明白了</p>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="cm">/**</span>
<span class="cm"> * Adds the specified element to this set if it is not already present.</span>
<span class="cm"> * More formally, adds the specified element &lt;tt&gt;e&lt;/tt&gt; to this set if</span>
<span class="cm"> * this set contains no element &lt;tt&gt;e2&lt;/tt&gt; such that</span>
<span class="cm"> * &lt;tt&gt;(e==null&amp;nbsp;?&amp;nbsp;e2==null&amp;nbsp;:&amp;nbsp;e.equals(e2))&lt;/tt&gt;.</span>
<span class="cm"> * If this set already contains the element, the call leaves the set</span>
<span class="cm"> * unchanged and returns &lt;tt&gt;false&lt;/tt&gt;.</span>
<span class="cm"> *</span>
<span class="cm"> * @param e element to be added to this set</span>
<span class="cm"> * @return &lt;tt&gt;true&lt;/tt&gt; if this set did not already contain the specified</span>
<span class="cm"> * element</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">add</span><span class="o">(</span><span class="n">E</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">return</span> <span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">e</span><span class="o">,</span> <span class="n">PRESENT</span><span class="o">)==</span><span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div>
<ul>
<li>load factor</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">public</span> <span class="nf">HashSet</span><span class="o">(</span><span class="n">Collection</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">E</span><span class="o">&gt;</span> <span class="n">c</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">map</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;(</span><span class="n">Math</span><span class="o">.</span><span class="na">max</span><span class="o">((</span><span class="kt">int</span><span class="o">)</span> <span class="o">(</span><span class="n">c</span><span class="o">.</span><span class="na">size</span><span class="o">()/.</span><span class="mi">75</span><span class="n">f</span><span class="o">)</span> <span class="o">+</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">16</span><span class="o">));</span>
        <span class="n">addAll</span><span class="o">(</span><span class="n">c</span><span class="o">);</span>
    <span class="o">}</span>
</code></pre></div>
<p>初始化中，注意使用的 <code>HashMap</code> 的 load factor 设置为 0.75，如果太小，就设置成 16. 为什么要 0.75 呢？ 有什么依据吗？</p>
 
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/22/read-openjdk-src-HashSet.html"> Read More ... </a> </br>
      </div>

    </li>
    
    <li>
      <div class="post-title">
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/21/read-openjdk-src-LinkedList.html"> OpenJDK 源码阅读之 LinkedList</a> </br>
      </div>

      <div class="post-info">
        <ol>
          <li class="post-time">2014-05-21</li> 
          <li class="post-category"> 
           技术&nbsp&nbspjava&nbsp&nbsp源代码阅读
          </li>
        </ol>
      </div>

      <div class="post-preview">
        <h1>OpenJDK 源码阅读之 LinkedList</h1>

<hr>

<h2>定义</h2>
<div class="highlight"><pre><code class="java language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">LinkedList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span>
    <span class="kd">extends</span> <span class="n">AbstractSequentialList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span>
    <span class="kd">implements</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;,</span> <span class="n">Deque</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;,</span> <span class="n">Cloneable</span><span class="o">,</span> <span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">Serializable</span>
<span class="o">{</span>
<span class="o">}</span>
</code></pre></div>
<h2>盲点</h2>

<ul>
<li>serialVersionUID</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">serialVersionUID</span> <span class="o">=</span> <span class="mi">876323262645176354L</span><span class="o">;</span>
</code></pre></div>
<p>序列化版本号，如果前一版本序列化后，后一版本发生了很大改变，就使用这个号告诉虚拟机，不能反序列化了。</p>

<h2>问题</h2>

<ul>
<li>writeObject</li>
</ul>

<p>比例一下 <code>ArrayList</code> 与 <code>LinkedList</code> 中的 writeObject</p>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">writeObject</span><span class="o">(</span><span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">ObjectOutputStream</span> <span class="n">s</span><span class="o">)</span>
        <span class="kd">throws</span> <span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">IOException</span><span class="o">{</span>
        <span class="c1">// Write out element count, and any hidden stuff</span>
        <span class="kt">int</span> <span class="n">expectedModCount</span> <span class="o">=</span> <span class="n">modCount</span><span class="o">;</span>
        <span class="n">s</span><span class="o">.</span><span class="na">defaultWriteObject</span><span class="o">();</span>

        <span class="c1">// Write out array length</span>
        <span class="n">s</span><span class="o">.</span><span class="na">writeInt</span><span class="o">(</span><span class="n">elementData</span><span class="o">.</span><span class="na">length</span><span class="o">);</span>

        <span class="c1">// Write out all elements in the proper order.</span>
        <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="o">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="n">size</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span>
            <span class="n">s</span><span class="o">.</span><span class="na">writeObject</span><span class="o">(</span><span class="n">elementData</span><span class="o">[</span><span class="n">i</span><span class="o">]);</span>

        <span class="k">if</span> <span class="o">(</span><span class="n">modCount</span> <span class="o">!=</span> <span class="n">expectedModCount</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ConcurrentModificationException</span><span class="o">();</span>
        <span class="o">}</span>

    <span class="o">}</span>
</code></pre></div><div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">writeObject</span><span class="o">(</span><span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">ObjectOutputStream</span> <span class="n">s</span><span class="o">)</span>
        <span class="kd">throws</span> <span class="n">java</span><span class="o">.</span><span class="na">io</span><span class="o">.</span><span class="na">IOException</span> <span class="o">{</span>
        <span class="c1">// Write out any hidden serialization magic</span>
        <span class="n">s</span><span class="o">.</span><span class="na">defaultWriteObject</span><span class="o">();</span>

        <span class="c1">// Write out size</span>
        <span class="n">s</span><span class="o">.</span><span class="na">writeInt</span><span class="o">(</span><span class="n">size</span><span class="o">);</span>

        <span class="c1">// Write out all elements in the proper order.</span>
        <span class="k">for</span> <span class="o">(</span><span class="n">Node</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="n">first</span><span class="o">;</span> <span class="n">x</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">;</span> <span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="na">next</span><span class="o">)</span>
            <span class="n">s</span><span class="o">.</span><span class="na">writeObject</span><span class="o">(</span><span class="n">x</span><span class="o">.</span><span class="na">item</span><span class="o">);</span>
    <span class="o">}</span>
</code></pre></div>
<p>注意后者没有检查　<code>modCount</code>，这是为什么呢？之前看 <code>ArrayList</code>的时候觉得是为线程安全考虑的，可是现在为什么又不检查了呢？虽然两个文件的注释中都说到，如果有多个线程操作此数据结构，应该从外部进行同步。但是一个检查，一个不检查是几个意思呀？</p>

<h2>思考</h2>

<ul>
<li>维护数据结构一致性</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">linkFirst</span><span class="o">(</span><span class="n">E</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">final</span> <span class="n">Node</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="n">f</span> <span class="o">=</span> <span class="n">first</span><span class="o">;</span>
        <span class="kd">final</span> <span class="n">Node</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="n">newNode</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Node</span><span class="o">&lt;&gt;(</span><span class="kc">null</span><span class="o">,</span> <span class="n">e</span><span class="o">,</span> <span class="n">f</span><span class="o">);</span>
        <span class="n">first</span> <span class="o">=</span> <span class="n">newNode</span><span class="o">;</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">f</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span>
            <span class="n">last</span> <span class="o">=</span> <span class="n">newNode</span><span class="o">;</span>
        <span class="k">else</span>
            <span class="n">f</span><span class="o">.</span><span class="na">prev</span> <span class="o">=</span> <span class="n">newNode</span><span class="o">;</span>
        <span class="n">size</span><span class="o">++;</span>
        <span class="n">modCount</span><span class="o">++;</span>
    <span class="o">}</span>
</code></pre></div>
<p>注意代码第 <code>5</code>行对 <code>f</code> 的检查，<code>6</code> 行对 <code>last</code> 的调整。一定要细心，保证操作后， <code>所有可能</code> 涉及的数据都得到相应更新。</p>

<ul>
<li>隐藏实现</li>
</ul>
<div class="highlight"><pre><code class="java language-java" data-lang="java">    <span class="kd">public</span> <span class="n">E</span> <span class="nf">getFirst</span><span class="o">()</span> <span class="o">{</span>
        <span class="kd">final</span> <span class="n">Node</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="n">f</span> <span class="o">=</span> <span class="n">first</span><span class="o">;</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">f</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="nf">NoSuchElementException</span><span class="o">();</span>
        <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="na">item</span><span class="o">;</span>
    <span class="o">}</span>
</code></pre></div>
<p>注意返回的是数据，而不是<code>Node</code>，外部根本不需要知道 <code>Node</code> 的存在。
另外，为什么 <code>f == null</code> 要抛出异常而不是返回 <code>null</code>？</p>
 
        <a href="/%E6%8A%80%E6%9C%AF/java/%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB/2014/05/21/read-openjdk-src-LinkedList.html"> Read More ... </a> </br>
      </div>

    </li>
    
</ul>

<!-- Pagination links -->

<div class="pagination">
  
    <a href="/page2">&laquo; Prev</a>
  

  
    
      <a href="/index.html">1</a>
    
  
    
      <a href="/page2">2</a>
    
  
    
      3
    
  
    
      <a href="/page4">4</a>
    
  
    
      <a href="/page5">5</a>
    
  
    
      <a href="/page6">6</a>
    
  
    
      <a href="/page7">7</a>
    
  
    
      <a href="/page8">8</a>
    
  
    
      <a href="/page9">9</a>
    
  
    
      <a href="/page10">10</a>
    
  
    
      <a href="/page11">11</a>
    
  

  
    <a href="/page4">Next &raquo;</a>
  
</div>



      </article>

      <div class="comments">
        
      </div>


      <footer>
        Copyright (c) minixalpha 2014
      </footer>

    </div>
  </body>
</html>
